#ifndef _AUDIOHOSTFORASIO_H_
#define _AUDIOHOSTFORASIO_H_
#include "AudioHost.h"
#include "wavvsthost.h"
#include "asiosys.h"
#include "asio.h"
#include "asiodrivers.h"

#if defined(_WIN32) || defined(_WIN64)
#ifdef _WINDOWS_
#else
#include <windows.h>
#endif
#include <MMReg.h>
#else
#ifndef _WAVEFORMATEX_
#define _WAVEFORMATEX_
typedef struct tWAVEFORMATEX
{
    unsigned short	wFormatTag;        /* no used if system is unix based */
    unsigned short	nChannels;         /* number of channels (i.e. mono, stereo...) */
    unsigned long	nSamplesPerSec;    /* sample rate */
    unsigned long	nAvgBytesPerSec;   /* for buffer estimation */
    unsigned short	nBlockAlign;       /* block size of data */
    unsigned short	wBitsPerSample;    /* Number of bits per sample of mono data */
    unsigned short	cbSize;            /* no used if system is unix based */
} WAVEFORMATEX,*PWAVEFORMATEX;
#endif /* _WAVEFORMATEX_ */
#endif

class CAudioHostForASIO:
	public CAudioHost
{
public:
	CAudioHostForASIO(void);
	~CAudioHostForASIO(void);
protected:
	//pthread_t CreateWorkThreads(LPTHREAD_START_ROUTINE lpThreadProc);
protected:
	int					m_nDevs;
	char				m_pszDevNames[16][128];
	bool				m_fIsRun;
	int					m_iCurDev;
	CWavVstHost**		m_pVSTArray;
	int					m_nBufferSize;
	int					m_iInChannels;
	int					m_nVolume;
	WAVEFORMATEX		m_wfx;
	int					m_nVST;

	long				MAX_BUFFER_SIZE;
	long				MIN_BUFFER_SIZE;

	bool				m_fVstChanged;

protected:
	bool Init();
	void Uninit();

	static ASIOTime* bufferSwitchTimeInfo(ASIOTime *timeInfo, long index, ASIOBool processNow);
	static void bufferSwitch(long index, ASIOBool processNow);
	static void sampleRateChanged(ASIOSampleRate sRate);
	static long asioMessages(long selector, long value, void* message, double* opt);

	bool CreateBuffer();
	void DeleteBuffer();
public:
	int GetListenDeviceCount(void);
	int GetCaptureDeviceCount(void);
	char* GetListenDeviceName(int i);
	char* GetCaptureDeviceName(int i);

	int GetCurrentListenDevice(void);
	bool SetCurrentListenDevice(int i);
	bool SetCurrentListenDeviceByName(char* szDeviceName);
	int GetCurrentCaptureDevice(void);
	bool SetCurrentCaptureDevice(int i);
	bool SetCurrentCaptureDeviceByName(char* szDeviceName);
	bool Start(void);
	void Stop(void);

	bool SetVstEffect(CWavVstHost** pVSTArray,int nCount);

	bool SetBufferSize(int size);
	int GetBufferSize(){return (int)(m_nBufferSize*m_wfx.nBlockAlign);};
	int GetBufferSizeBySampleCount(){return (int)m_nBufferSize;};
	BOOL SetBufferSizeBySampleCount(int samples){return SetBufferSize(samples*m_wfx.nBlockAlign);};
	int SetVolume(int Vol);//0-100
	int GetVolume(void);
	bool SetFormat(WAVEFORMATEX* pwfx);
	void GetFormat(WAVEFORMATEX* pwfx);
};
#endif